
package rich.string;

import java.io.UnsupportedEncodingException;

import org.junit.Test;

public class TestString {

	@Test
	public void testEncoding() throws UnsupportedEncodingException{
		 String chars = "ni我\uff0c\u8bf4\u201c\u6765\uff0c\u770b\u4e00\u4f1a\u80fd\u4e0d\u80fd\u751f\u51fa\u4e2a\u5c0f\u7684\u6765\u2026\u751f";
		 
		 System.out.println(chars);
		 String chars_enGB2312 = new String(chars.getBytes("GB2312"), "GB2312");
		 System.out.println( chars.equals(chars_enGB2312));//true!
		 String chars_enUTF8 = new String(chars.getBytes("UTF-8"), "UTF-8");
		 System.out.println( chars.equals(chars_enUTF8));//true!
		 String chars_enSJIS = new String(chars.getBytes("SJIS"), "SJIS");
	}
	
	@Test
	/*
	 * for twitter. Parse the tweet
	 */
	public void testIndexof(){
		String text = "I love this url at: http://www.163.com.  He is the best!";
		System.out.println("test text is : "+ text);
		int httpIndex = text.indexOf("http://");
		System.out.println("index of http:// is : " + httpIndex);
		int spaceIndex = text.indexOf(" ",httpIndex);
		System.out.println("index of \' \' is : " + spaceIndex);
		
		System.out.println("url result is : " + text.substring(httpIndex,spaceIndex));
	}
	
	
	@Test
	public void testEmpty(){
		String test = " ";
		
		System.out.println("string is \""+ test +"\" and is empty?" + test.isEmpty());
		System.out.println("string is \""+ test +"\" and trim() is empty?" + test.trim().isEmpty());
	}
	
	@Test
	public void testStringEquals(){
		String test = new Integer(123).toString();
		String expectString = "123";
		String expectString2 = "123";
		System.out.println(test.hashCode());
		System.out.println(expectString.hashCode());
		System.out.println(expectString == test);
		System.out.println(expectString.equals(test));
	}
	
	@Test
	public void testLastIndex(){
		String fileName = "1ss";
		int lastDot = fileName.lastIndexOf('.');
		System.out.println("ext is :"+fileName.substring(lastDot + 1));
		
	}
	
	@Test
	public void testSplit(){
		String test = "1,2,3,4,5, ,6,,";
		String[] result = test.split(",");
		System.out.println("start-----------");
		for (String string : result) {
			System.out.println("result :|||" + string.trim()+"|||");
		}
		System.out.println("end--------------");
	}
	
	
	@Test
	public void testUnicode(){
		String unicode = "这是一个测试unicode的例子";
		System.out.println(unicode.length());
		System.out.println(unicode.substring(1));
		System.out.println(unicode.charAt(0));
		
	}
	
	
	@Test
	public void testEquals(){
		String a = "test";
		String b1 = getStringa();
		String b2 = getStringb();
		String c = "test";
		System.out.println("a.equals(b1) is " + a.equals(b1));
		System.out.println("a.equals(b2) is " + a.equals(b2));
		System.out.println("a == b1 is : "+(a == b1));
		System.out.println("a == b2 is : "+(a == b2));
		System.out.println("a == c is : "+(a == c));
		
		new StringBuffer(a).reverse();
	}

	/**
	 * @function
	 * @return
	 *
	 * @author
	 * @history
	 *
	 */
	private String getStringa() {
		String a = "test";
		return a;
	}
	private String getStringb() {
		
		return "teststring".substring(0,4);
	}
}
